<template>
	<view class="pages">
		<u-navbar title="仓位" :autoBack="true" placeholder bgColor="#fff"></u-navbar>
		<view class="top-search">
      <u-search
        placeholder="搜索仓位名称"
        v-model="searchValue"
        @search="search"
        :showAction="false"
      ></u-search>
		</view>
    <u-line></u-line>
    <view>
      <u-cell v-for="(item, index) in list" :key="index" :name="item.join(',')" :title="item[0] || '-'" :value="item[1] || ''" isLink @click="select"></u-cell>
    </view>
	</view>
</template>

<script>
import { getMaterialInformation } from '@/api/index'

export default {
  data() {
    return {
      dataList: [],
      list: [],
      orgInfo: {},
      isMore: true,
      StartRow: 0,
      Limit: 200,
      type: '',
      index: '',
      FNumber: '',
      searchValue: '',
    }
  },
  onLoad(options) {
    this.type = options.type
    this.index = options.index
    this.FNumber = options.FNumber
    this.getPage()
  },
  methods: {
    getPage(f = false) {
      uni.showLoading({ mask: true });
      const parameters = [
        {
          "FormId":"BD_STOCK",
          "FieldKeys":"FFlexEntryName,FFlexEntryId.fnumber,FNumber",
          "FilterString": "FNumber='"+ this.FNumber +"'",
          "OrderString":"fnumber ASC",
          "TopRowCount":0,
          "StartRow": this.Limit * this.StartRow,
          "Limit": this.Limit,
        }
      ]
      if(this.searchValue) {
        parameters[0].FilterString = parameters[0].FilterString + " and FFlexEntryName like '%"+ this.searchValue +"%'"
      }
      getMaterialInformation({parameters}).then(res => {
        uni.hideLoading();
        if(res.length < this.Limit) {
          this.isMore = false
        }
        if(f) {
          this.list = []
        }
        this.list = res
      })
    },
    search() {
      this.StartRow = 0
      this.getPage(true)
    },
    select(e) {
      const info = e.name.split(',')
      let pages = getCurrentPages()
      let prevPage = pages[pages.length - 2]
      if(this.type == '1') {
        prevPage.$vm.form.FSrcStockLocName = info[0]
        prevPage.$vm.form.FSrcStockLocId = info[1]
      } else if(this.type == '2') {
        prevPage.$vm.form.FDestStockLocName = info[0]
        prevPage.$vm.form.FDestStockLocId = info[1]
      } else if(this.type == '3') {
        if(info[1] == prevPage.$vm.form.FDestStockLocId) {
          uni.showToast({
            title: '原始货位和目标货位不能是一个',
            icon: 'none'
          });
          return
        }
        const index = this.index
        const item = prevPage.$vm.list1[index]
        item.FDestStockLocName = info[0]
        item.FDestStockLocId = info[1]
        prevPage.$vm.$set(prevPage.$vm.list1, index, item)
      } else if(this.type == '4') {
        if(info[1] == prevPage.$vm.form.FDestStockLocId) {
          uni.showToast({
            title: '原始货位和目标货位不能是一个',
            icon: 'none'
          });
          return
        }
        const index = this.index
        const item = prevPage.$vm.list2[index]
        item.FDestStockLocName = info[0]
        item.FDestStockLocId = info[1]
        prevPage.$vm.$set(prevPage.$vm.list2, index, item)
      } else if(this.type == '5') {
        const beforPage = prevPage.$vm
        const item = beforPage.goodsList[beforPage.goodsIndex];
        item.position = info[0]
        prevPage.$vm.$set(beforPage.goodsList, beforPage.goodsIndex, item)
      }
      this.goBack()
    },
  },
  onReachBottom() {
    if(this.isMore) {
      this.StartRow++
      this.getPage()
    }
  }
}
</script>

<style lang="scss" scoped>
.pages{
  width: 750rpx;
  min-height: 100vh;
  padding-bottom: 40rpx;
  .top-search {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 30rpx 20rpx;
    margin-top: 20rpx;
  }
}
</style>